using System;
using System.Collections.Generic;
using System.Text;
using Spacedash.Logic.Scenario.Cells;
using Barrage.Global;

namespace Spacedash.Logic.RaceStates
{
    /// <summary>
    /// Race State that processes logic when ships are racing
    /// </summary>
    public class RacingRaceState:RaceState 
    {
        /// <summary>
        /// \internal State identifier
        /// </summary>
        public override RaceStateList State
        {
            get { return RaceStateList.Racing; }
        }

        long startTime;
        /// <summary>
        /// Race elapsed time
        /// </summary>
        public long ElapsedTime { get { return Timer.DefaultTimer.TotalTime - startTime; } }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="race">Race that's creating this instance</param>
        public RacingRaceState(Race race)
            : base(race)
        {
            startTime = Timer.DefaultTimer.TotalTime;
            foreach (Ship s in race.Ships)
            {
                s.State = ShipState.Running;
                s.ClearLaps();
            }
        }
        /// <summary>
        /// \internal Updates logic
        /// </summary>
        public override void Update()
        {
            Scenario.Scenario scenario = race.Scenario;
            scenario.ClearChangedCells();
            bool raceFinished = true;
            List<Ship> shipPositions = new List<Ship>(race.PlayerNumber);
            
            foreach (Ship s in race.Ships)
            {
                if (s.State != ShipState.Running) continue;
                
                Cell cell = scenario.CellAt(s.ScenarioPosition);
                if (cell.CanBoost(s)) s.Boost();
                cell.ShipPassed(s);
                scenario.AddChangedCell(cell);
                scenario.ActivateCell(cell);

                CellData data = cell.CellData;
                if (data != null)
                    data.ExecuteCollisionActions(s);

                if (s.CurrentLapNumber >= race.LapNumber)
                {
                    s.RacePosition = race.AddFinishedShip(s);
                    s.State = ShipState.Finished;
                }
                else
                {
                    int i=0;
                    for (; i < shipPositions.Count; i++)
                    {
                        if (shipPositions[i].ScenarioPosition.Y > s.ScenarioPosition.Y)
                            break;                        
                    }
                    shipPositions.Insert(i, s);
                    raceFinished = false;
                }
            }
            int j=0;

            foreach (Ship s in shipPositions)
            {
                s.RacePosition = race.PlayerNumber - 1 - j;
                j++;
            }

            race.OrderShips();
            
            if (raceFinished)
                race.State = new FinishingRaceState(race);
            // Update ship positions

            
            scenario.UpdateCells();
        }
    }
}
